// 提交链接：https://leetcode.cn/problems/sqrtx/
// lc69. x的平方根
// 完成日期：2024/9/9
// c++ 二分查找

class Solution {
public:
    int mySqrt(int x) {
        int left = 0, right = x, mid, ans = 0;
        while (left <= right)
        {
            mid = left + (right - left) / 2;
            if ((long long)mid * mid > x)
                right = mid - 1;
            else 
            {
                left = mid + 1;
                ans = mid;
            }
        }
        return ans;
    }
};